package BNS.plumbing;

import java.awt.BorderLayout;
import java.io.Serializable;

import javax.swing.JApplet;


/** This class is the base of the applet that operates on the player machine.
	It can be used to support early game development by standing in for the
	whole server/SessionHost/game/ClientLiaison/Client apparatus.
	Change all the lines herein that have *HACK THIS* on them.
	*/
public class TestClient extends JApplet implements Runnable {
	/**
   * 
   */
  private static final long serialVersionUID = 1L;

  BNSPersona persona;
	
	private int terminateReason;
	private int clientID;
	private Thread clientThread;
	private String playerName;
	
	/** Initializes the applet and installs some type of BNSPersona.
		This method is required by the Applet interface. */
	public void init() {
		playerName= getParameter("playername");
		getContentPane().setLayout(new BorderLayout());
		persona= new TextPersona("test Client for "+playerName);	// *HACK THIS*
/*		persona= new coloring.ColoringPersona(		// another example
						  null	// ClientLiaison, aka game
						, testGraph
						, 3		// number of colors
						, 20 	// time limit (secs)
						, 53	// perfection
						, true	// colormapIsIdentity
						, true	// showNodes
						, "payoff definition string"
						, new String[]{"$1.00","$2.25"}
						);  /**/			
		getContentPane().add("Center",persona.startPanel());
		validate();
		clientID= -1;
		}
	
	/** Attempts to resign from the game.
		This method is part of the Applet interface. */
	public void destroy() { 
		System.err.println("Client destroy()ed!");
		if (persona!=null) remove(persona.killPanel());
		}
	
	/* creates and starts a Thread to operate the persona. Part of the Applet interface. */
	public void start() {
		System.err.println("Client start()ed");
		clientThread= new Thread(this);
		clientThread.start();
		}
	
	/* stops the Thread that operates the persona. Part of the Applet interface. */
	public void stop() {
		System.err.println("Client stop()ed!!!!");
		terminateReason= 3;
		clientThread.interrupt();  // knocks run() out of its wait()s
		}
	
	/** Contains a few calls to test drive GUI.	Replace them with calls that
		are appropriate for testing your new GUI.	*/
	public synchronized void run() {
		terminateReason= 0;
		String msg;		
		try {
			this.wait(1000); // just wait a sec
			msg= "hi";
			System.err.println(msg);
			persona.gameUpdate((Serializable)(msg+"\n"));	// *HACK THIS*
			
			this.wait(1000); // just wait a sec
			msg= "why?";
			System.err.println(msg);
			persona.gameUpdate((Serializable)(msg+"\n"));	// *HACK THIS*
			
			this.wait(1000); // just wait a sec
			msg= "bye";
			System.err.println(msg);
			persona.gameUpdate((Serializable)(msg+"\n"));	// *HACK THIS*
			
			this.wait(); // just wait until stop()ed
			}
		catch (InterruptedException ie) { 
			if(terminateReason!=3) System.err.println("clockThread interrupted; WHY?"); }
		System.err.println("run finished");
		} // EOMethod run()
	
	} // EOC Client
